package com.shxc.smarttrade.old.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.myview.AvatarImageView;
import com.shxc.smarttrade.old.utils.BaseView;
import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.shxc.smarttrade.old.utils.BaseView.showPopupWindow;

/**
 * Created by jcdev1 on 2017/5/12.
 */

public class PersonalCenterActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_avator)
    LinearLayout llAvator;
    @BindView(R.id.avatar_img)
    AvatarImageView avatarImageView;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_position)
    TextView tvPosition;
    private String name = "";//昵称
    private String age = "";
    private String sex = "男";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_center);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    /**
     * 初始化view
     */
    private void initView() {
        tvCenter.setText(getResources().getString(R.string.base_inform));
        avatarImageView.setTitleColor("#444444");
        avatarImageView.setLineColor("#d9d9d9");
        avatarImageView.setBtnTextColor("#d89b3c");
        avatarImageView.setTitleLineColor("#999999");
        setAfterCropListener();

    }

    /**
     * 裁剪结束后回调
     */
    private void setAfterCropListener() {
        avatarImageView.setAfterCropListener(new AvatarImageView.AfterCropListener() {
            @Override
            public void afterCrop(Bitmap photo) {

            }
        });
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        rlLeft.setOnClickListener(this);
        llAvator.setOnClickListener(this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1314 && resultCode == 520) {
            name = data.getStringExtra("name");
            tvName.setText(name);

        } else {//在拍照、选取照片、裁剪Activity结束后，调用的方法
            if (avatarImageView != null)
                avatarImageView.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_left:
                finish();
                break;
            case R.id.ll_avator:
                //选取头像
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.ll_name, R.id.ll_age, R.id.ll_sex, R.id.ll_position})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_name:
                Intent intent = new Intent(this, ChangeNameActivity.class);
                startActivityForResult(intent, 1314);
                break;
            case R.id.ll_age:
                chooseAge();
                break;
            case R.id.ll_sex:
                chooseSex();
                break;
            case R.id.ll_position:
                break;
        }
    }

    /**
     * 选择性别
     */
    private void chooseSex() {
        View view1 = View.inflate(this, R.layout.pop_style_choose_sex, null);
        TextView tvCancel = (TextView) view1.findViewById(R.id.tv_cancel);
        TextView tvFinish = (TextView) view1.findViewById(R.id.tv_finish);
        final ImageView ivMan = (ImageView) view1.findViewById(R.id.iv_man);
        final ImageView ivWoman = (ImageView) view1.findViewById(R.id.iv_woman);
        LinearLayout llMan = (LinearLayout) view1.findViewById(R.id.ll_man);
        LinearLayout llWoman = (LinearLayout) view1.findViewById(R.id.ll_woman);
        final PopupWindow popupWindow = BaseView.showPopupWindow(view1, this, R.layout.activity_personal_center);
        llMan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sex = "男";
                ivMan.setVisibility(View.VISIBLE);
                ivWoman.setVisibility(View.INVISIBLE);
            }
        });
        llWoman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sex = "女";
                ivMan.setVisibility(View.INVISIBLE);
                ivWoman.setVisibility(View.VISIBLE);
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        tvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                tvSex.setText(sex);
            }
        });
    }

    /**
     * 选择年纪
     */
    private void chooseAge() {
        final List<String> wheelDatas = new ArrayList<>();
        for (int i = 18; i < 100; i++) {
            if (i == 18) {
                wheelDatas.add("18岁以下");
            } else {
                wheelDatas.add(i + "岁");
            }
        }
        View view1 = View.inflate(this, R.layout.pop_style_choose_age, null);
        TextView tvCancel = (TextView) view1.findViewById(R.id.tv_cancel);
        TextView tvFinish = (TextView) view1.findViewById(R.id.tv_finish);
        final WheelView wheelView = (WheelView) view1.findViewById(R.id.wheelview);
        wheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        wheelView.setSkin(WheelView.Skin.Holo);
        wheelView.setWheelData(wheelDatas);
        final PopupWindow popupWindow = showPopupWindow(view1, this, R.layout.activity_personal_center);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        tvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvAge.setText(wheelDatas.get(wheelView.getCurrentPosition()));
                age = wheelDatas.get(wheelView.getCurrentPosition());
                popupWindow.dismiss();
            }
        });
    }
}
